/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database.dao;

import database.common.GenericDao;
import database.entity.Customers;
import database.entity.Orders;
import java.util.Date;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Administrator
 */
public class OrdersDao extends GenericDao<Orders, Integer> {
    
    public List<Orders> search(Date start, Date end, String customer, String order, int status) {
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        Criteria c = session.createCriteria(Orders.class);
        if (customer != null && !customer.isEmpty()) {
            c.createAlias("customer", "customer_alias").
                    add(Restrictions.disjunction().
                    add(Restrictions.like("customer_alias.name", "%" + customer + "%")));
//            c.add(Restrictions.like("customer.name", customer));
        }
        if (order != null && !order.isEmpty()) {
            c.add(Restrictions.like("name", "%" + order + "%"));
        }
        if (start != null) {
            c.add(Restrictions.ge("createdAt", start));
        }
        if (end != null) {
            c.add(Restrictions.le("deliveredAt", end));
        }
        if (status >= 0) {
            c.add(Restrictions.eq("status", status));
        }
        List<Orders> list = c.list();
        session.flush();
        session.getTransaction().commit();
        return list;
    }
    
    public List<Orders> getAllByCustomer(Customers aThis) {
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        Criteria c = session.createCriteria(Orders.class);
        c.add(Restrictions.eq("customer", aThis));
        List<Orders> list = c.list();
        session.flush();
        session.getTransaction().commit();
        return list;
    }
    
    public List<Orders> getAllAvailable() {
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        Criteria c = session.createCriteria(Orders.class);
        c.add(Restrictions.ne("status", 2));
        List<Orders> list = c.list();
        session.flush();
        session.getTransaction().commit();
        return list;
    }
}
